<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class LM_Table_Eventtype extends LM_Table
{
    /** @var int Primary Key */
    public $eventtype_id;
    /** @var string title */
    public $eventtype_title;
    /** @var string name */
    public $eventtype_name;
    /** @var string playermap_association */
    public $playermap_association;
    /** @var string icon */
    public $eventtype_icon;
    /** @var int ordering */
    public $ordering;

    /**
     * constructor
     * @param object $db mosDatabase
     */
    public function __construct($db)
    {
        parent::__construct('#__leagueman_eventtype', 'eventtype_id', $db);
    }

    public function getID()
    {
        return $this->eventtype_id;
    }

    public function getTitle()
    {
        return $this->eventtype_title;
    }

    public function getName()
    {
        return $this->eventtype_name;
    }

    public function getAssociation()
    {
        return $this->playermap_association;
    }

    public function getIcon()
    {
        return $this->eventtype_icon;
    }

    public function getIconImage()
    {

        $icon = $this->getIcon();

        if (!empty($icon)) {
            return '<img src="' . $GLOBALS["mosConfig_live_site"] . '/images/stories/leagueman/eventtypes/' . $icon . '" border="0"/>';
        } else {
            return $this->getName();
        }
    }

    public function getOrdering()
    {
        return $this->ordering;
    }

    public function getPlayermapAssociations()
    {

        $this->_db->setQuery("DESCRIBE #__leagueman_playermap");
        $fields = $this->_db->loadObjectList();

        $associations = array();
        foreach ($fields AS $field) {
            if (substr($field->Field, 0, 3) == "cnt") {
                $associations[] = $field->Field;
            }
        }

        return $associations;
    }

    public function getRows($field = "")
    {

        $sql = "SELECT * FROM #__leagueman_eventtype ORDER BY ordering";

        $this->_db->setQuery($sql);

        return $this->_db->loadObjectList($field);
    }

    public function getEventtypes()
    {

        $eventtypes = array();

        foreach ($this->getRows() AS $row) {

            $eventtype = LM::getLMTable("Eventtype");
            $eventtype->populate($row);

            $eventtypes[$row->eventtype_id] = $eventtype;
        }

        return $eventtypes;
    }

    public function check()
    {

        if (empty($this->eventtype_name)) {
            return false;
        }

        if (empty($this->playermap_association)) {
            return false;
        }

        if (substr($this->playermap_association, 0, 4) != "cnt_") {
            $this->playermap_association = "cnt_" . $this->playermap_association;
        }

        if (in_array($this->playermap_association, $this->getPlayermapAssociations())) {
            return false;
        }

        return true;
    }

}